package cn.dada.dadarecipes.community.favorites.controller;

import cn.dada.dadarecipes.base.response.JsonResult;
import cn.dada.dadarecipes.community.favorites.pojo.dto.FavoritesRemoveParam;
import cn.dada.dadarecipes.community.favorites.pojo.dto.FavoritesSaveParam;
import cn.dada.dadarecipes.community.favorites.pojo.vo.FavoritesVO;
import cn.dada.dadarecipes.community.favorites.service.FavoritesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@Api(tags = "02.03文章收藏管理")
@RestController
@RequestMapping("/v1/article/favorites/")
public class FavoritesController {
    @Autowired
    private FavoritesService favoritesService;

    /**
     * 控制器-文章收藏列表查询
     * @param userId
     * @param session
     * @return
     */
    @GetMapping("select")
    @ApiOperation("文章收藏列表查询")
    @ApiImplicitParam(name = "userId", value = "用户id", required = true)
    public JsonResult selectFavorites(Long userId,@ApiIgnore HttpSession session) {
        log.debug("控制器-查询收藏列表: userId:{},{}",userId,session);
        List<FavoritesVO> list = favoritesService.selectFavorites(userId,session);
        return JsonResult.ok(list);
    }

    /**
     * 控制器-文章收藏
     * @param favoritesSaveParam
     * @param session
     * @return
     */
    @PostMapping("save")
    @ApiOperation("文章收藏")
    public JsonResult saveFavorites(FavoritesSaveParam favoritesSaveParam, @ApiIgnore HttpSession session) {
        log.debug("控制器-收藏文章: favoritesParam:{},{}", favoritesSaveParam,session);
        favoritesService.saveFavorites(favoritesSaveParam,session);
        return JsonResult.ok();
    }

    /**
     * 控制器-取消收藏
     * @param favoritesRemoveParam
     * @param session
     * @return
     */
    @PostMapping("remove")
    @ApiOperation("取消收藏")
    public JsonResult removeFavorites(FavoritesRemoveParam favoritesRemoveParam, @ApiIgnore HttpSession session) {
        log.debug("控制器-取消收藏: favoritesRemoveParam:{},{}", favoritesRemoveParam,session);
        favoritesService.removeFavorites(favoritesRemoveParam,session);
        return JsonResult.ok();
    }
}
